<?php

class Friendship_IndexController extends Zend_Controller_Action
{

    public function init()
    {
		$this->_helper->layout()->setLayout('abc');
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	//echo 'aaaaaaaaaaaaaaaa';
    	$fs = new Friendship_Model_Dbtable_Friendship();
    	echo '<pre>';
    		print_r($fs->getFriends('1'));
    	echo '</pre>';	
    }
	
	public function addfriendAction()
	{
		$this->getHelper('viewRenderer')->setNoRender();
		$user1 = $_POST['user1'];
		$user2 = $_POST['user2'];
		$af = new Friendship_Model_Dbtable_RequestFriendship();
		$af -> addrequestfriends($user1,$user2);
		
		$user = new User_Model_DbTable_Users();
		$name_f_r = $user -> get_fname_lname_ava_user($user1);
		$kq=array('firstname'=>$name_f_r[0]['user_firstname'],'lastname'=>$name_f_r[0]['user_lastname'],'avata'=>$name_f_r[0]['user_avata']);
		echo Zend_Json::encode($kq);
	}

	public function testAction()
	{
	/*
		$this->getHelper('viewRenderer')->setNoRender();
		$cfs = new Friendship_Model_Dbtable_RequestFriendship();
		print_r ($cfs -> checkrequestfriendships('1'));
		*/
		$this->getHelper('viewRenderer')->setNoRender();
		echo '123';
	}
	
	//Lay so luong request chua doc
	public function getnumrequestfriendshipsAction(){
		$this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
		
		$iduser = $_POST['iduser'];
		$crf = new Friendship_Model_Dbtable_RequestFriendship();
		$num_friend_request = $crf ->getrequestfriendships($iduser);
		
		$kq=array('num_friend_request'=>$num_friend_request);
		echo Zend_Json::encode($kq);
		
	}
	
	//lay thong tin cua request friend de hien thi o mua request friend
	public function getrequestfriendshipsAction(){
		$this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
		
		$requestfriendship = new Friendship_Model_Dbtable_RequestFriendship;
		$user = new User_Model_DbTable_Users;
		$iduser = $_POST['iduser'];
		$id_request_friend = $requestfriendship -> getinforequestfriendships($iduser);

		$count = count($id_request_friend);
		$html_friend_request="";
		
		for($i=0; $i<$count; $i++)
		{
			$info_user = $user -> get_fname_lname_ava_user($id_request_friend[$i]['request_friendship_user1']);
		
			$id = $info_user[0]['id'];
			$user_firstname = $info_user[0]['user_firstname'];
			$user_lastname = $info_user[0]['user_lastname'];
			$user_avata = $info_user[0]['user_avata'];

			$html_friend_request .= "<li class='cursor_d bd_bottom request_ketban_".$id."'><div class='img_user_tb'><img src='/hoidap/Source/public/images/ava.jpg' alt='' title='user'/></div><div class='nd_tb'><div class='title_tb'>".$user_lastname." ".$user_firstname."</div><div class='date_tb'>Khoảng 3 ngày trước</div><div class='xacnhan_kb_1 xn_kb_".$id."'><a onclick='xacnhanketban(".$id.",".$iduser.");' href='javascript:;'>Xác nhận</a></div><div class='xacnhan_kb_2 kpln_kb_".$id."'><a onclick='khongphailucnay(".$id.",".$iduser.");' href='javascript:;'>Không phải lúc này</a></div></div></li>";
		}
		echo $html_friend_request;
	}
	
	
	//Ajax 2s se su ly 1 lan
	public function test2Action(){
		$this->getHelper('viewRenderer')->setNoRender();
		$abc = new Friendship_Model_Dbtable_Friendship();
		$a = $abc->getFriends('16');
		print_r($a);
	}
	
	public function acceptrequestfriendsAction(){
		$this->getHelper('viewRenderer')->setNoRender();
		echo $iduser1 = $_POST['iduser1'];
		echo $iduser2 = $_POST['iduser2'];
		
		$acp = new Friendship_Model_Dbtable_RequestFriendship();
		$acp->acceptrequestfriends($iduser1, $iduser2);
	}
	
	public function unacceptrequestfriendsAction(){
		$this->getHelper('viewRenderer')->setNoRender();
		echo $iduser1 = $_POST['iduser1'];
		echo $iduser2 = $_POST['iduser2'];
		
		$uacp = new Friendship_Model_Dbtable_RequestFriendship();
		$uacp ->unacceptrequestfriends($iduser1, $iduser2);
	}
	
	


}

